package com.lujieni.myredis.redis.storage;

import com.lujieni.myredis.redis.ICacheStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Package: com.lujieni.myredis.redis.storage
 * @ClassName: RedisCacheStorage
 * @Author: lujieni
 * @Description: 1
 * @Date: 2021-02-19 17:07
 * @Version: 1.0
 */
@Component
public class RedisCacheStorage<K,V> implements ICacheStorage<K,V> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean set(K key, V value, int num) {
         redisTemplate.opsForValue().set(key,value,num, TimeUnit.SECONDS);
         return true;
    }

    @Override
    public boolean set(K key, V value) {
        redisTemplate.opsForValue().set(key,value);
        return true;
    }

    @Override
    public V get(K key) {
        if(key == null)
            throw new RuntimeException("key为空!");
        return (V) redisTemplate.opsForValue().get(key);
    }
}